using System.Collections.Generic;

namespace LP
{
    public abstract class AbsPoolEntry
    {
        public abstract void OnTake();
        public abstract void OnRecycle();
    }

    public abstract class AbsPool<T> : ILifecycle where T : AbsPoolEntry
    {
        protected Queue<T> cacheEntrys = new Queue<T>();
        public int GrowthCount { get; private set; }
        public AbsPool(int growthCount)
        {
            GrowthCount = growthCount;
        }

        public abstract void Growth();

        public T Take()
        {
            if (cacheEntrys.Count == 0)
            {
                Growth();
            }
            var entry = cacheEntrys.Dequeue();
            entry.OnTake();
            return entry;
        }

        public void Recycle(T entry)
        {
            entry.OnRecycle();
            cacheEntrys.Enqueue(entry);
        }

        public virtual void OnInit()
        {
        }

        public virtual void OnDestroy()
        {
            cacheEntrys.Clear();
        }

        public virtual void OnInitFinish()
        {
        }
    }
}